package jp.co.sharp.android.parents.kidsguard.common;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;

public class DateTimePickerDlg extends Dialog {

    private NumberPicker mDlgAmPmSpinner;
    private NumberPicker mDlgHourSpinner;
    private NumberPicker mDlgMinuteSpinner;
    private Context mContext;
    private int mAmPm, mHour, mMinute;
    private OnDateTimeSetListener mOnDateTimeSetListener;
    private DateTimePickerDlg dateTimePickerDlg;
    private Integer pAmPm, pHour, pMinute;

    public DateTimePickerDlg(@NonNull Context context, Integer pAmPm, Integer pHour, Integer pMinute) {
        super(context);
        mContext = context;
        dateTimePickerDlg = this;
        this.pAmPm = pAmPm;
        this.pHour = pHour;
        this.pMinute = pMinute;
    }

    public DateTimePickerDlg(@NonNull Context context, int themeResId, Integer pAmPm, Integer pHour, Integer pMinute) {
        super(context, themeResId);
        mContext = context;
        dateTimePickerDlg = this;
        this.pAmPm = pAmPm;
        this.pHour = pHour;
        this.pMinute = pMinute;
    }

    protected DateTimePickerDlg(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener, Integer pAmPm, Integer pHour, Integer pMinute) {
        super(context, cancelable, cancelListener);
        mContext = context;
        dateTimePickerDlg = this;
        this.pAmPm = pAmPm;
        this.pHour = pHour;
        this.pMinute = pMinute;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dlg_datetime_picker);

        initDlg();
    }

    private void initDlg() {
        mDlgAmPmSpinner = (NumberPicker) this.findViewById(R.id.dlgAmPm);
        List<String> pDlgAmPm = new ArrayList();
        pDlgAmPm.add("午前");
        pDlgAmPm.add("午後");
        String[] strDlgAmPm = new String[pDlgAmPm.size()];
        mDlgAmPmSpinner.setDisplayedValues(pDlgAmPm.toArray(strDlgAmPm));
        mDlgAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
        mDlgAmPmSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mDlgAmPmSpinner.setMinValue(0);
        mDlgAmPmSpinner.setMaxValue(strDlgAmPm.length - 1);
        mDlgAmPmSpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgAmPmSpinner);
        if (null != pAmPm) {
            mDlgAmPmSpinner.setValue(pAmPm);
        }

        DecimalFormat decimalFormat = new DecimalFormat("00");
        mDlgHourSpinner = (NumberPicker) this.findViewById(R.id.dlgHour);
        List<String> pDlgHour = new ArrayList();
        for (int i =0; i < 12; i++) {
            pDlgHour.add(decimalFormat.format(i));
        }
        String[] strDlgHour = new String[pDlgHour.size()];
        mDlgHourSpinner.setDisplayedValues(pDlgHour.toArray(strDlgHour));
        mDlgHourSpinner.setMinValue(0);
        mDlgHourSpinner.setMaxValue(strDlgHour.length - 1);
        mDlgHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
        mDlgHourSpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgHourSpinner);
        if (null != pHour) {
            mDlgHourSpinner.setValue(pHour);
        }

        mDlgMinuteSpinner = (NumberPicker) this.findViewById(R.id.dlgMinute);
        List<String> pDlgMinute = new ArrayList();
        for (int i =0; i < 60; i++) {
            pDlgMinute.add(decimalFormat.format(i));
        }
        String[] strDlgMinute = new String[pDlgMinute.size()];
        mDlgMinuteSpinner.setDisplayedValues(pDlgMinute.toArray(strDlgMinute));
        mDlgMinuteSpinner.setMinValue(0);
        mDlgMinuteSpinner.setMaxValue(strDlgMinute.length - 1);
        mDlgMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
        mDlgMinuteSpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgMinuteSpinner);
        if (null != pMinute) {
            mDlgMinuteSpinner.setValue(pMinute);
        }

        Button buttonOK = (Button)this.findViewById(R.id.yes);
        buttonOK.setOnClickListener(new ListenerOK());

        Button buttonCancel = (Button)this.findViewById(R.id.cancel);
        buttonCancel.setOnClickListener(new ListenerCancel());
    }

    /**
     *
     * 控件监听器
     */
    private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mAmPm = newVal;
        }
    };

    private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mHour = newVal;
        }
    };

    private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mMinute = newVal;
        }
    };

    /**
     *
     */
    public interface OnDateTimeSetListener {
        void onDateTimeSet(int pAmPm, int pHour, int pMinute);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }

    class ListenerOK implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnDateTimeSetListener != null) {
                mOnDateTimeSetListener.onDateTimeSet(mDlgAmPmSpinner.getValue(), mDlgHourSpinner.getValue(), mDlgMinuteSpinner.getValue());
                dateTimePickerDlg.dismiss();
            }
        }
    }

    class ListenerCancel implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnDateTimeSetListener != null) {
                dateTimePickerDlg.dismiss();
            }
        }
    }

    /**
     * 设置picker分割线的颜色
     */
    private void setDividerColor(NumberPicker picker) {
        Field field = null;
        try {
            field = NumberPicker.class.getDeclaredField("mSelectionDivider");
            if (field != null) {
                field.setAccessible(true);
                field.set(picker, new ColorDrawable(ContextCompat.getColor(mContext, R.color.colorDlgTimePicker)));
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

}
